package com.sx.micowell.framework.model

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.CacheDiskUtils
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.SPUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.cache.CacheBean
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.*
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.SPKey
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction
import io.reactivex.Observable

/**
 * @Author: YY
 * @Time: 2021/8/20
 * @Describe TODO
 */
class PaymentModel : BaseViewModel() {

    var setMealId: String = ""

    var payId: Int = 0

    var paymentListData: MutableLiveListData<PayModeVO> = MutableLiveListData()

    var openMemberDataEvent : SingleLiveEvent<String> = SingleLiveEvent()

    //用户协议数据
    var vipNotesField: ObservableField<VipNotesVO> = ObservableField()

    //是否勾选服务协议
    var isCheckAgreement = ObservableField(false)


    //提交支付
    var submitPayOnClickCommand = BindingCommand<Any>(IBindingAction {
        if (isCheckAgreement.get() == false) {
            ToastUtils.showShort("请勾选会员协议")
            return@IBindingAction
        }
        LogUtils.d("submitPayOnClickCommand: $payId")
        openMember(payId, setMealId)
    })

    //是否勾选服务协议
    var isCheckAgreementOnClickCommand: BindingCommand<Boolean> = BindingCommand { isChecked ->
        isCheckAgreement.set(isChecked)
    }

    //点击协议事件
    var agreementOnClickCommand: BindingCommand<IBindingAction> = BindingCommand(IBindingAction {
        val bundle = Bundle()
        bundle.putSerializable(IntentKey.DATA, vipNotesField.get())
        context.startContainerActivity(
            RouteManager.getInstance().getMemberAgreementFragment(vipNotesField.get()).javaClass.canonicalName,
            bundle
        )
    })


    fun reqData(model: Boolean) {
        val modelStr : String = if (model) "App" else "Web"
        addSubscribe(Observable.merge(
            HttpClient.instance.apiService.payModeInfo(modelStr),
            HttpClient.instance.apiService.vipNotes()
        ).compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<Any?>() {
                override fun success(data: Any?, msg: String?) {
                    when (data) {
                        is PayModeWrap -> {
                            paymentListData.value = data.paymode
                        }
                        is VipNotesVO -> {
                            vipNotesField.set(data)
                        }
                    }
                }
                override fun onFail(e: Throwable?, errorMsg: String?) {

                }

            })
        )
    }

    /**
     * 开通会员
     */
    private fun openMember(payId: Int, setMealId: String) {
        LogUtils.d("openMember: $payId -- $setMealId")
        addSubscribe(HttpClient.instance.apiService
            .openMember(setMealId, payId)
            .doOnSubscribe { ui.showDialog("正在加载...") }
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<OpenMemberVO?>() {
                override fun success(data: OpenMemberVO?, msg: String?) {
                    LogUtils.d("openMember: ${data?.form}")
                    openMemberDataEvent.value = data?.form
                    ui.hideDialog()
                }
                override fun onFail(e: Throwable?, errorMsg: String?) {
                    ui.hideDialog()
                }

            })
        )

    }

    fun userInfo() {

        addSubscribe(HttpClient.instance.apiService
            .userInfo()
            .doOnSubscribe {
            }
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<UserInfoVO>() {

                override fun success(data: UserInfoVO?, msg: String?) {
                    data?.let {
                        SPUtils.getInstance().put(SPKey.SP_VIP, it.isVip)
                    }

                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                }

            })
        )

    }
}